package com.example.content_android.ui.articleList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.content_android.R;
import com.nostra13.universalimageloader.utils.L;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.Response;

public class ArticleListFragment extends Fragment implements AdapterView.OnItemClickListener {
    ListView listView;
    SimpleAdapter simpleAdapter;

    public List<Map<String, Object>> list = new ArrayList<>();
    //    private ArticleListViewModel articleListViewModel;
    private com.example.content_android.ui.home.ArticleListFragment.OnFragmentInteractionListener mListener;

    @Override
    @SuppressLint("NewApi")
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {
            initData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initData() throws IOException {
        Response response = OkHttpUtils
                .get()//
                .url("https://www.turkeylike.cn:8900/fzulab/article/list")//
                .addParams("limit", "100")
                .tag(this)//
                .build()//
                .execute();

        JSONObject jsonObject = JSONObject.parseObject(response.body().string());

        if (jsonObject.getInteger("code") == 500) {
            String msg = jsonObject.getString("msg");

            Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            return;

        }
        if (jsonObject.getInteger("code") == 0) {
            JSONObject result = jsonObject.getJSONObject("result");
            JSONArray fileArray = result.getJSONArray("list");


            if (fileArray != null && fileArray.size() > 0) {
                for (int i = 0; i < fileArray.size(); i++) {
                    JSONObject str = JSON.parseObject(fileArray.get(i).toString());

                    Map map = new HashMap();
                    map.put("article_title", str.get("title").toString());
                    map.put("article_detail", "查看详情");
                    map.put("article_content", str.get("content").toString());
                    map.put("article_remark", str.get("remark").toString());
                    map.put("article_createDate", str.get("createDate").toString());
                    map.put("article_createName", str.get("createName").toString());


                    list.add(map);
                }
            }

        }

    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_article_list, container, false);
        listView = root.findViewById(R.id.fragment_article_list_view);

        simpleAdapter = new SimpleAdapter(getActivity(), getData(), R.layout.article_list_item, new String[]{"article_title", "article_detail"}, new int[]{R.id.article_title, R.id.article_detail});
        listView.setAdapter(simpleAdapter);

        listView.setOnItemClickListener(this);
        return root;
    }

    private List<Map<String, Object>> getData() {
//        String [] titles={"水果1","水果2"};
//         String [] detail={"查看详情","查看详情"};
//        List<Map<String,Object>> list= new ArrayList<>();
//        for(int i=0;i<2;i++){
//            Map  map = new HashMap();
//            map.put("article_title",titles[i]);
//            map.put("article_detail",detail[i]);
//            list.add(map);
//        }
        return list;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        L.i("position:" + position);


        String articleDetail = listView.getAdapter().getItem(position).toString();
        L.i("position:" + position + "text" + articleDetail);
        ArrayList<String> list =
                new ArrayList<String>(Arrays.asList(articleDetail.split(",")));
        ArticleDetailBean articleDetailBean =new ArticleDetailBean();
        HashMap<String,String> map=(HashMap<String,String>)parent.getItemAtPosition(position);
        Set<Map.Entry<String, String>> en=map.entrySet();
        for (Map.Entry<String, String> entry : en) {
            String key=entry.getKey();
            String value=entry.getValue();
            if("article_title".equals(key)){
                articleDetailBean.setTitle(value);
            }
            if("article_content".equals(key)){
                articleDetailBean.setContent(value);
            }
            if("article_remark".equals(key)){
                articleDetailBean.setRemark(value);
            }
            if("article_createDate".equals(key)){
                articleDetailBean.setCreateDate(value);
            }
            if("article_createName".equals(key)){
                articleDetailBean.setCreateName(value);
            }

            System.out.println(key+"->>>"+value);

        }

        System.out.println(map);
        System.out.println(articleDetailBean);

//        //Fragment 隐藏、切换
//        FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
//        transaction.hide(ArticleListFragment.this);
//        Fragment ff = new ArticleDetailFragment();
//        transaction.add(R.id.nav_host_fragment, ff);
//        transaction.show(ff);
//        transaction.commit();

        Intent intent=new Intent(getActivity(), ArticleDetailActivity.class);
        intent.putExtra("articleDetail", articleDetailBean);

        startActivity(intent);



    }
}